// pages/signin/signin.js
var appData = getApp().globalData;
Page({
  data:{
    mobile:'',
      fstPwd:'',
      secPwd:'',
      hidden:true,
      modalText:'',
      Loading:true
  },
  onLoad(option){
    this.data.mobile = option.mobile;
  },
  fstInput:function(e){
    this.setData({
      fstPwd: e.detail.value
    })
  },
  secInput:function(e){
    this.setData({
      secPwd: e.detail.value
    })
  },
  resetpwd:function(){
    const reg = /^[a-zA-Z0-9]{6,20}$/;
    var self = this;
    console.log(self.data);
    if(!reg.test(self.data.fstPwd)){
      self.setData({
        hidden:false,modalText:"密码格式不正确，密码至少6位只能输入下划线、英文、数字"
      })
      return;
    };
    if(self.data.fstPwd!=self.data.secPwd){
      self.setData({
        hidden:false,modalText:"两次输入的密码不一致"
      })
      return;
    }
    self.setData({Loading:false})
   getApp().request({
    url: 'register',
    data: {
      mobile:self.data.mobile,
      psw:self.data.fstPwd,
      nick_name:appData.nickName,
      open_id:appData.openid,
      icon:appData.avatarUrl,
      sex:appData.gender
    },
    success: function(res){
      console.log(self.data.mobile);
      console.log(self.data.fstPwd);
      console.log(appData.nickName);
      console.log(appData.openid);
      console.log(appData.avatarUrl);
      console.log(appData.gender);
      if(res.data.flag){
        self.setData({
          Loading:true
        })
        try {
          wx.reLaunch({
            url: '../accountlogin/login',
          })
        } catch (e) {
          wx.redirectTo({
            url: '../accountlogin/login',
          })
        }
      }else{
        self.setData({
        modalText:res.data.msg,
        hidden:false,
        Loading:true
      })
      }
    },
    fail: function(res) {
      self.setData({
        modalText:'请求失败，请稍后再试',
        hidden:false,
        Loading:true
      })
    }
  })
  },
  confirm:function(){
    this.setData({
      hidden:true
    })
  }
})
